package com.hzy.potrace.demo;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.StringUtils;

/**
 * Created by huzongyao on 18-1-31.
 */

public class BitmapUtils {

    public static Bitmap getBitmapFromUri(Context context, Uri url, int maxWidth, int maxHeight) {
        String path = getPathFromUri(context, url);
        if (StringUtils.isTrimEmpty(path)) return null;
        return ImageUtils.getBitmap(path, maxWidth, maxHeight);
    }

    public static String getPathFromUri(Context context, Uri uri) {
        if (null == uri) return null;
        final String scheme = uri.getScheme();
        String data = null;
        if (scheme == null || ContentResolver.SCHEME_FILE.equals(scheme))
            data = uri.getPath();
        else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
            Cursor cursor = context.getContentResolver().query(uri, new String[]
                    {MediaStore.Images.ImageColumns.DATA}, null, null, null);
            if (null != cursor) {
                if (cursor.moveToFirst()) {
                    int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
                    if (index > -1) {
                        data = cursor.getString(index);
                    }
                }
                cursor.close();
            }
        }
        return data;
    }
}
